<?php namespace helpers;
use models\Mysql as Mysql;
/**
 * This code is property of Piotr Poradzisz. Do not distribute under threat of legal consequences.
 * All rights reserved.
 * Все права защищены.
 * Alle Rechte vorbehalten.
 * Todos los derechos reservados.
 * Wszelkie prawa zastrzeżone.
 *                  جميع الحقوق محفوظة
 * 保留所有权利
 * すべての著作権
 * @author Piotr Poradzisz
 * email: piotr.poradzisz@gmail.com
 * tel.: +48668027020
 */
class Helper {
    public static function multiDimensionalSearch($array, $key, $value){
        foreach($array as $key1 => $value1){            
            if(is_array($value1) and $value1[$key] == $value) return $key1;
        }
        return false;
    }

    public static function convertToASCI($string){
        $a = array  (   'Ę', 'Ó', 'Ą', 'Ś', 'Ł', 'Ż', 'Ź', 'Ć', 'Ń',
                        'ę', 'ó', 'ą', 'ś', 'ł', 'ż', 'ź', 'ć', 'ń',
                        'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß', " ",
                        '!', '@', '#', ':', '$', '%', '^',
                        "{", "}", "'", ";", ',', '+', "'"

                    );
        $b = array  (   'E', 'O', 'A', 'S', 'L', 'Z', 'Z', 'C', 'N',
                        'e', 'o', 'a', 's', 'l', 'z', 'z', 'c', 'n',
                        'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', '_',
                        '-', '-', '-', '-', '-', '-', '-',
                        '-', '-', '-', '-', '-', '-', "`"


                    );
        $string = str_replace($a, $b, $string);
        return $string;
    }

    public static function convertToPath($string) {
        return  str_replace("\-", "-", preg_replace("/-{2,32}/", "-", self::convertToASCI(strtolower($string))));
    }

    public static function convertToLink($string){
        $a = array  (   'Ę', 'Ó', 'Ą', 'Ś', 'Ł', 'Ż', 'Ź', 'Ć', 'Ń',
                        'ę', 'ó', 'ą', 'ś', 'ł', 'ż', 'ź', 'ć', 'ń',
                        'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß',
                        '-', '!', '@', '#', '$', '%', '^', '+', ':', " ", ','
                    );
        $b = array  (   'E', 'O', 'A', 'S', 'L', 'Z', 'Z', 'C', 'N',
                        'e', 'o', 'a', 's', 'l', 'z', 'z', 'c', 'n',
                        'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss',
                        '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_'
                    );
        $string = strtolower(str_replace($a, $b, $string));
        return $string;
    }

    public static function convertToShellPath($string){
        $a = array  (   " "
                    );
        $b = array  (   "\ "
                    );
        echo $string = strtolower(str_replace($a, $b, $string));
        return $string;
    }

    public static function createNewItem($tableName) {
        $mysql = new Mysql();
        $query = "INSERT INTO $tableName () VALUES ()";
        $mysql->setQuery($query);
        $query = "SELECT LAST_INSERT_ID()";
        return $mysql->getRecord($query);
    }

    public static function multiDemisionArraySort($array, $on, $order=SORT_ASC){
        $new_array = array();
        $sortable_array = array();

        if (count($array) > 0) {
            foreach ($array as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $k2 => $v2) {
                        if ($k2 == $on) {
                            $sortable_array[$k] = $v2;
                        }
                    }
                } else {
                    $sortable_array[$k] = $v;
                }
            }

            switch ($order) {
                case SORT_ASC:
                    asort($sortable_array);
                break;
                case SORT_DESC:
                    arsort($sortable_array);
                break;
            }

            foreach ($sortable_array as $k => $v) {
                $new_array[$k] = $array[$k];
            }
        }
        return $new_array;
    }

    public static function getDictionaryValues($tableName, $id = null, $parent = false) {
        $mysql = new Mysql();
        if(isset($id)){
            if($parent === true){
                echo $query = "SELECT CONCAT_WS(' -> ',t2.name, t1.name) as name FROM $tableName t1 JOIN $tableName t2 ON t1.parent = t2.id WHERE t1.id = $id";echo "<br>";
            }
            else{
                $query = "SELECT name FROM $tableName WHERE id = $id";
            }
            return $mysql->getRecord($query);
        }
        else {
            if($parent === true){
                $query = "SELECT t1.id, t1.parent, CONCAT_WS(' -> ',t2.name, t1.name) as name FROM $tableName t1 JOIN $tableName t2 ON t1.parent = t2.id";
            }
            else{
                $query = "SELECT id, name FROM $tableName";
            }
            return $mysql->getTable($query);
        }
    }
    
    public static function WriteToFile($fileName, $content)
    {
        $FileHandle = fopen($fileName, 'a');
        fwrite($FileHandle, $content);
        fwrite($FileHandle, "\n");
        fclose($FileHandle);
    }

}
?>
